/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import controllers.InvitationManager;
import controllers.SearchManager;
import controllers.UserManager;
import entities.Event;
import entities.User;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.validation.constraints.NotNull;

/**
 *
 * @author Alberto, Alessandra
 */
@ManagedBean
@ViewScoped
public class InviteBean {
    
    @EJB
    SearchManager sm;
    
    @EJB
    InvitationManager im;
    
    @EJB
    UserManager um;
    
    private Event event;
    
    //@NotNull(message = "Insert username")
    private String username;
    
    private List<User> invited;
    
    private List<User> users;
     
    private User selectedUser;

    public List<User> getInvited() {
        return invited;
    }

    public void setInvited(List<User> invited) {
        this.invited = invited;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }
 
    public List<User> getUsers() {
        return users;
    }
 
    public User getSelectedUser() {
        return selectedUser;
    }
 
    public void setSelectedUser(User selectedUser) {
        this.selectedUser = selectedUser;
    }
    
    public InviteBean () {
        this.users = new ArrayList<User>();
        this.invited = new ArrayList<User>();
        this.event = (Event) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("event");
    }
    
    public void search () {
        
        users.clear();
        users.addAll(im.searchForInvite(username, event));
        
    }
    
    public void invite () {
        im.invite(event, selectedUser);
        users.clear();
        invited.add(selectedUser);
    }
    
    public String exit () {
        return "calendar?faces-redirect=true";
    }
    
}
